const path = require('path')

const HtmlWebpackPlugin = require('html-webpack-plugin')
const {CleanWebpackPlugin} = require('clean-webpack-plugin')
const MiniCSSExtractPlugin = require('mini-css-extract-plugin')
const OptimizeCSSAssetWebpackPlugin = require('optimize-css-assets-webpack-plugin')
const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin')

module.exports = {
  // 配置控制台输出等级
  stats: 'errors-only',
  devServer: {
    contentBase: './dist',
    hot: true,
    // 在 devServer 中也配置控制台的输出等级
    stats: 'errors-only'
  },
  devtool: 'sourcemap',
  entry: './src/main.js',
  output: {
    filename: 'bundle_[hash:8].js',
    path: path.resolve(__dirname, 'dist')
  },
  module: {
    rules: [
      {
        test: /.(css|sass|scss)$/,
        use: [
          MiniCSSExtractPlugin.loader,
          'css-loader',
          {
            loader: 'px2rem-loader',
            options: {
              remUnit: 75,
              remPrecision: 8
            }
          },
          'sass-loader'
        ]
      },
      {
        test: /.(png|svg|jpg|jpeg|gif|webp)$/,
        use: {
          loader: 'file-loader',
          options: {
            name: 'img/[name]-[hash:8].[ext]'
          }
        }
      }
    ]
  },
  plugins: [
    new CleanWebpackPlugin(),
    // 构建时提供命令行显示
    new FriendlyErrorsWebpackPlugin()
  ]
}
